package com.tedu.myboot.controller;

import com.tedu.myboot.entity.Article;
import com.tedu.myboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    private static File articleDir;
    static {
        articleDir = new File("./articles");
        if(!articleDir.exists()){
            articleDir.mkdirs();
        }
    }
    @RequestMapping("/myweb/writeArticle")
    public void writeArticle(Article article, HttpServletResponse response){

        System.out.println(article.getTitle()+","+article.getAuthor()+","+article.getContent());

        //2保存文章内容
        Article article1 = new Article(article.getTitle(), article.getAuthor(), article.getContent());
        File articleFile = new File(articleDir,article.getTitle()+".obj");
        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(articleFile)
                )
        ){
            oos.writeObject(article);

            response.sendRedirect("/myweb/article_success.html");

        } catch (IOException e) {
            e.printStackTrace();
        }



    }
    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request,HttpServletResponse response){
        //1将articles目录下的所有obj文件进行反序列化，得到所有的文章信息
        List<Article> articleList = new ArrayList<>();
        File[] articles = articleDir.listFiles(f->f.getName().endsWith(".obj"));
        for(File articleFile : articles){
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(
                                    articleFile
                            )
                    )
            ){
                Article article = (Article)ois.readObject();
                articleList.add(article);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        /*
            2生成一个HTML页面(动态页面)并包含所有文章数据
         */
        //添加响应头，用于将来告知浏览器该动态数据是一个html页面
        response.setContentType("text/html;charset=utf-8");

        PrintWriter pw = null;
        try {
            pw = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>文章列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>标题</td>");
        pw.println("<td>作者</td>");
        pw.println("</tr>");
        for(Article article : articleList) {
            pw.println("<tr>");
            pw.println("<td>"+ article.getTitle()+"</td>");
            pw.println("<td>"+ article.getAuthor()+"</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");
    }


}
